package com.masterrabbit.mcslf.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.masterrabbit.common.core.utils.StringUtils;
import com.masterrabbit.common.core.web.domain.AjaxResult;
import com.masterrabbit.common.security.utils.SecurityUtils;
import com.masterrabbit.mcslf.contans.Constants;
import com.masterrabbit.mcslf.mapper.PackagesMapper;
import com.masterrabbit.mcslf.mapper.ServicePackageMapper;
import com.masterrabbit.mcslf.mapper.ServicesMapper;
import com.masterrabbit.mcslf.pojo.Packages;
import com.masterrabbit.mcslf.service.PackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("package")
public class PackageController {
    @Autowired
    private PackagesMapper packagesMapper;
    @Autowired
    private ServicePackageMapper servicePackageMapper;
    @Autowired
    private ServicesMapper servicesMapper;

    @Autowired
    private PackagesService packagesService;
    @RequestMapping("/list")
    public AjaxResult getPackageList(@RequestBody HashMap<String, Object> map, HttpServletRequest request) {
        System.out.println(map.toString());
        IPage page = new Page<Packages>((Integer)map.get("current"),(Integer)map.get("size"));
        QueryWrapper<Packages> packagesQueryWrapper = new QueryWrapper<>();
        packagesQueryWrapper.eq("merchant_id",request.getHeader(Constants.CUURRENT_STORE));
        IPage page1 = packagesMapper.selectPage(page, packagesQueryWrapper);
        return AjaxResult.success(page1);
    }
    @RequestMapping("/add")
    public AjaxResult addPackage(@RequestBody HashMap<String, Object> map,HttpServletRequest request) {
        String ids1 = map.get("ids").toString();
        Object packageInfo = map.get("packageInfo");
        String s = JSON.toJSONString(packageInfo);
        Packages packages = null;
        try {
            packages = JSON.parseObject(s, Packages.class);
        } catch (Exception e) {
            return AjaxResult.error("请输入正确信息");
        }
        if (!StringUtils.isNotEmpty(packages.getPackageName())){
            return AjaxResult.error("套餐名不能为空");
        }
        if (packages.getPrince()==null){
            return AjaxResult.error("请正确输入价格");
        }
        List<Integer> strings = JSON.parseArray(ids1).toList(Integer.class);
//        添加前端未传输属性
        packages.setPackageCode(UUID.randomUUID().toString());
        packages.setMerchantId(request.getIntHeader(Constants.CUURRENT_STORE));
        packagesService.add(packages,strings);
        return AjaxResult.success();
    }

}
